Contents | Index | < Browse | Browse >

LETTERiosULETTER A base class for stream manipulation and attaching of stream buffers to streams.

Overview
#include <iostream.h>

class ios {
public:
enum io_state {
goodbit = 0x00,
eofbit = 0x01,
failbit = 0x02,
badbit = 0x04,
hardfail = 0x80
};
enum open_mode {
in = 0x01,
out = 0x02,
ate = 0x04,
app = 0x08,
trunc = 0x10,
nocreate = 0x20,
noreplace = 0x40,
binary = 0x80
};
enum seek_dir {
beg = -1,
cur = 0,
end = 1
};
enum {
skipws = 0x00000001,
left = 0x00000002,
right = 0x00000004,
internal = 0x00000008,
dec = 0x00000010,
oct = 0x00000020,
hex = 0x00000040,
showbase = 0x00000080,
showpoint = 0x00000100,
uppercase = 0x00000200,
showpos = 0x00000400,
scientific = 0x00000800,
fixed = 0x00001000,
unitbuf = 0x00002000,
stdio = 0x00004000,
firstfreebit = 0x00008000
};
ios(streambuf *b);
virtual ~ios();
unsigned long flags();
unsigned long flags(unsigned long f);
unsigned long setf(unsigned long f);
unsigned long unsetf(unsigned long f);
unsigned long setf(unsigned long f, unsigned long m);
int width();
int width(int w);
ostream *tie();
ostream *tie(ostream *o);
char fill();
char fill(char f);
int precision();
int precision(int p);
int rdstate();
int eof();
int fail();
int bad();
int good();
void clear(int i = 0);
operator void *();
int operator !();
streambuf* rdbuf();
static void sync_with_stdio();
static unsigned long bitalloc();
static int xalloc();
long &iword(int i);
void *&pword(int i);
static const unsigned long basefield;
static const unsigned long adjustfield;
static const unsigned long floatfield;
protected:
ios();
void init(streambuf *);
streambuf *aBuf;
int aState;
ostream *aTie;
short int aPrecision;
char aFill;
short aWidth;
unsigned long aFlags;
};

Portability
AT&T Release 2 streams library

Description
The ios class is the base class for all stream classes, particularly ostream, istream and iostream. It defines a couple of enumerations used to define a stream's status, its stream mode, its format flags for in- and output and the stream's seek direction. Furthermore it binds in- and output streams together and allows for extension of the stream definition for new format methods and status variables through derived classes.

Types
enum ios::io_state {
goodbit, eofbit, failbit, badbit, hardfail
};

These status flags represent the stream's status.

goodbit is no flag but a symbolic name for the status when all other flags are cleared, that is, the stream's status is okay.

eofbit is set if the input encountered an EOF.

failtbit is set by an in- or output error, eg. formatting error.

badbit is also set by an in- or output error. If it's set, it does not make sense to continue I/O on this stream any longer.

hardfail is set when everything's broken and the stream is inrepairable.

enum ios::open_mode {
in, out, ate, app, trunc, nocreate, noreplace, binary
};

These flags can be specified when opening a file.

in opens the file for read access.

out opens the file for write access.

ate opens the file and sets the byte cursor to the end of the file.

app opens the file for appending, any output will be appended to the end of the file.

trunc opens the file and clears its length to 0.

nocreate prevents creating a new file if none exists.

noreplace prevents replacing an already existing file.

binary is useless on the Amiga, as DOS does not differ between text- and binary files.

Some combinations of these flags are pretty useless (eg. ios::nocreate| ios::noreplace). As soon as file is opened for write access, it will be exclusively locked, regardless whether an old file is opened or a new file is created.

enum ios::seek_dir {
beg, cur, end
};

These enumerations specify the relative position for a seek in a stream.

beg starts the seek at the beginning of the stream.

cur starts the seek from the current position.

end starts the seek from the end of the stream.

enum {
skipws, left, right, internal, dec, oct, hex, showbase,
showpoint, uppercase, showpos, scientific, fixed,
unitbuf, stdio, firstfreebit
};

These flags specify the formatting used for the next I/O.

skipws skips whitespaces when reading, this includes spaces, tabs, linefeeds etc.

left justifies the output leftbound, if the field width is longer than the output.

Appropiately, right justifies the output right-bound.

internal causes padding between the sign and/or a base indicator and the number. If the number has no sign or no sign is written, justification occurs rightbound.

dec specifies decimal (base 10) as conversion base, which is the default.

oct specifies octal (base 8) as conversion base.

hex specifies hexadecimal (base 16) as conversion base.

showbase causes the output to show the base of a number: "0" for octal values, "0x" or "0X" for hexadecimal values. Otherwise the number is assumed to be a decimal number.

showpoint forces the output of a decimal point of floating-point numbers.

uppercase causes uppercase letters to be used for base indicators and scientific notations.

showpos forces the output of a sign, even if the number is positive. The sign will be appropiately either "+" or "-".

scientific causes a floating-point value to be always converted to scientific notation.

fixed causes a floating-point value to be always converted to decimal notation.

If none of these two flags is set, the format used depends on the size and the precision of the number.

unitbuf causes the stream to flush after every formatted output. Not recommended.

stdio and firstfreebit are internal flags.

The format flags left, right and internal together build the adjustfield group, dec, oct and hex build the basefield group and scientific and fixed build the floatfield group.

Constructors
ios::ios(streambuf *);
Initializes the stream object and binds the specified stream buffer to the stream. The pointer to the buffer should not be zero as this effect is undefined.

ios::ios(); Initializes the stream object without binding a stream buffer.

Deconstructors
ios::~ios();
Is used to build a virtual deconstructor and therefore also closes the stream.

Methods
unsigned long ios::flags();
Returns the current formatting flags.

unsigned long ios::flags(unsigned long f);
Resets the formatting flags and returns the old flags.

unsigned long ios::setf(unsigned long f);
Sets new formatting flags and returns the old status of these flags.

unsigned long ios::unsetf(unsigned long f);
Clears the formatting flags and returns the old status of these flags.

unsigned long ios::setf(unsigned long f, unsigned long m);
Sets a group of formatting flags and returns the old status of these flags. Some flags together build a group, in which only one flag must be set at the same time. The ios::basefields, ios::floatfield and ios::adjustfield attributes each describe a mask valid for these groups.

int ios::width();
Returns the current field width.

int ios::width(int w);
Sets the field width and returns the old value. The field with specifies the minimum characters for a formatted output, however if the output itself is longer than the field width it will not be cut. If the field needs to be padded ios::adjustflags's formatting flags are used.

ostream *ios::tie();
Returns the output stream the stream is bound to.

ostream *ios::tie(ostream *);
Sets the output stream the stream is bound to and returns the old stream (the previously bound stream or NULL). Usually new streams are only tied to istreams. This binding is particularly useful for cin and cout as the output stream is flushed before each input to allow for formatted in- and output.

Example

#include <iostream.h>

void main()
{
int number;
cout << "Please enter a number: ";
cin >> number;
}

If cout weren't tied to cin, cout would not be flushed and the text would not be written - you'd have to force this using cout << flush.

char ios::fill();
Returns the padding character used to fill the field if the field width is larger than the characters written.

char ios::fill(char f);
Sets a new padding character and returns the old character.

int ios::precision();
Returns the precision used for outputting floating-point numbers.
int ios::precision(int);
Sets a new precision for floating-point numbers and returns the old value.

int ios::rdstate();
Returns the status of the stream. The status is reset at every I/O operation. If an error occured, further I/O operations will fail.

int ios::eof();
Returns ios::eofbit if an EOF was encountered during the last input operation and 0 otherwise.

int ios::fail();
Returns a value higher or lower than zero if an error occured during the last in- or output operation.

int ios::bad();
Returns a value higher or lower than zero if a heavy error occured during the last in- or output operation.

int ios::good();
Returns 1 if the stream is okay and no EOF has been read and 0 otherwise. Further I/O operations will fail if this method does not return 1.

void ios::clear(int i = 0);
Clears the status normally to 0. You should not use this function to make further I/O on the broken stream possible as you'll only get more errors.

ios::operator void *();
Returns the object itself if the stream has no error (that is, !fail()) and zero otherwise.

int ios::operator !();
Equals fail().

streambuf* ios::rdbuf();
Returns the stream buffer tied to this stream.

static void ios::sync_with_stdio();
Synchronizes the stream with the stdio (that is, it flushes the standard I/O buffers).

static unsigned long ios::bitalloc();
Returns the next unused formatting flag or 0 if no flag is free any longer. The reservation is valid for all stream objects.

static int ios::xalloc();
Returns the index of a new status variable. The reservation is valid for all stream objects.

long &ios::iword(int i);
Returns a reference to the status variable with the status i, which has been previously reserved using ios::xalloc().

void *&ios::pword(int i);
Returns a reference to the status variable with the status i, which has been previously reserved using ios::xalloc(). The difference to ios::iword is that the status variable is treated as a pointer.

See also
istream , ostream